package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysUser;
import cn.stylefeng.guns.modular.yingbao.entity.SysUserGroup;
import cn.stylefeng.guns.modular.yingbao.entity.UserMy;
import cn.stylefeng.guns.modular.yingbao.mapper.SysUserMapper;
import cn.stylefeng.guns.modular.yingbao.mapper.UserMyMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.UserMyParam;
import cn.stylefeng.guns.modular.yingbao.model.result.UserMyResult;
import cn.stylefeng.guns.modular.yingbao.service.SysUserGroupService;
import  cn.stylefeng.guns.modular.yingbao.service.UserMyService;
import cn.stylefeng.guns.modular.yingbao.util.QrCodeUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 我的用户表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-14
 */
@Service
@Slf4j
public class UserMyServiceImpl extends ServiceImpl<UserMyMapper, UserMy> implements UserMyService {


    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysUserGroupService sysUserGroupService;

    @Override
    public void add(UserMyParam param){
        UserMy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserMyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserMyParam param){
        UserMy oldEntity = getOldEntity(param);
        UserMy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserMyResult findBySpec(UserMyParam param){
        return null;
    }

    @Override
    public List<UserMyResult> findListBySpec(UserMyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserMyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData addMyUser(UserMy myUser) {
        myUser.setVipGrade(0);
        myUser.setVersion(1);

        //myUser.setBackup2("http://192.168.1.116:81/index.html?referrerCode="+myUser.getId());
        myUser.setBackup2("http://103.70.225.134:82/#/?referrerCode="+myUser.getId());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        QrCodeUtil.createQrCodeImage(myUser.getBackup2(),300,"/usr/local/yingbao/imges/code/"+uuid+".png");
        myUser.setBackup3("/code/"+uuid+".png");
        myUser.setBackup4("0");
        myUser.setBackup5("0");
        myUser.setBackup7(0L);
        myUser.setBackup8(0L);
        myUser.setBackup9(0L);
        int insert = this.baseMapper.insert(myUser);
        if(insert > 0){
            return ResponseData.success(200,"添加成功",null);
        }
        return ResponseData.error(500,"添加失败");
    }

    @Override
    public ResponseData queryById(Long id) {
        UserMy userMy = this.baseMapper.selectOne(new QueryWrapper<UserMy>().eq("backup6",id));
        if (userMy != null){
            return ResponseData.success(userMy);
        }
        return ResponseData.error(500,"查无此人");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData upByMyUser(String pic,String name,String id) {
        if( StringUtils.isEmpty(id)){
            return ResponseData.error("参数错误");
        }
        UserMy userMy1 = this.baseMapper.selectOne(new QueryWrapper<UserMy>().eq("backup6",id));
        if(userMy1 == null){
            return ResponseData.error(500,"暂无此人");
        }
        if (StringUtils.isNotEmpty(name)) {
            userMy1.setName(name);
            SysUserGroup userid = sysUserGroupService.getOne(new QueryWrapper<SysUserGroup>().eq("userid", userMy1.getBackup6()));
            userid.setUserName(name);
            int version = userid.getVersion();
            userid.setVersion(version+1);
            boolean update = sysUserGroupService.update(userid, new QueryWrapper<SysUserGroup>().eq("id", userid.getId()).eq("version", version));
            if(!update){
                throw new UnsupportedOperationException("错误！");
            }
        }
        if(StringUtils.isNotEmpty(pic)) {
            userMy1.setPic(pic);
            SysUserGroup userid = sysUserGroupService.getOne(new QueryWrapper<SysUserGroup>().eq("userid", userMy1.getBackup6()));
            userid.setAvatar(pic);
            int version = userid.getVersion();
            userid.setVersion(version+1);
            boolean update = sysUserGroupService.update(userid, new QueryWrapper<SysUserGroup>().eq("id", userid.getId()).eq("version", version));
            if(!update){
                throw new UnsupportedOperationException("错误！");
            }
        }
        Integer version = userMy1.getVersion();
        userMy1.setVersion(userMy1.getVersion()+1);
        int update = this.baseMapper.update(userMy1, new QueryWrapper<UserMy>().eq("id", userMy1.getId()).eq("version", version));
        if(update > 0){
            return ResponseData.success();
        }
        return ResponseData.error(500,"修改失败");
    }

    @Override
    public void VIPData() {
        Date date = new Date();
        List<UserMy> userMyLists = baseMapper.selectList(new QueryWrapper<UserMy>().ge("vip_grade",date));
        if(userMyLists.size() != 0){
            for(UserMy user:userMyLists){
                user.setVipAddTime(null);
                user.setVipDuration(null);
                user.setVipGrade(0);
                user.setVipTime(null);
                Integer version = user.getVersion();
                user.setVersion(user.getVersion()+1);
                SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("id", user.getBackup6()));
                Long version1 = sysUser.getVersion();
                sysUser.setVersion(sysUser.getVersion()+1);
                sysUser.setVipId(0L);
                sysUserMapper.update(sysUser,new QueryWrapper<SysUser>().eq("id",sysUser.getId()).eq("version",version1));
                baseMapper.update(user,new QueryWrapper<UserMy>().eq("id",user.getId()).eq("version",version));
            }
        }
        List<UserMy> userMyList = baseMapper.selectList(new QueryWrapper<UserMy>().lt("vip_grade",date));
        for(UserMy user:userMyList){
            if (user.getVipDuration()!=null){
                user.setVipDuration(user.getVipDuration()-1);
            }
        }
        boolean b = this.updateBatchById(userMyList);
        if(!b){
            log.error("批量更新错误",userMyList);
        }
    }


    private Serializable getKey(UserMyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserMy getOldEntity(UserMyParam param) {
        return this.getById(getKey(param));
    }

    private UserMy getEntity(UserMyParam param) {
        UserMy entity = new UserMy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }




}
